<?php

namespace app\admin\controller;

use app\admin\validate\ConfigValidate;
use app\service\ConfigService;
use think\exception\ValidateException;

class Config extends Admin
{
    public function __construct(ConfigService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function add()
    {
        if (!$this->request->post()) {
            return $this->fetch("form");
        }
        return $this->save();
    }

    public function index()
    {
        if (!$this->request->isPost()) {
            $this->assign("title", "配置列表");
            return $this->fetch("index");
        }
        $params = $this->request->post();

        $where = [];
        if (!empty($params["name"])) {
            $where[] = ['name', 'like', "%{$params['name']}%"];
        }
        if (!empty($params["title"])) {
            $where[] = ['title', 'like', "%{$params['title']}%"];
        }
        if (!empty($params["status"])) {
            $where[] = ['status', '=', $params["status"]];
        }
        $ret = $this->service->getList($where);
        $this->result($ret);
    }

    private function save()
    {
        $params = $this->request->Post();
        $data = [
            "name" => $params["name"],
            "id" => isset($params["id"]) ? $params["id"] : 0,
            "sort" => $params["sort"],
            "title" => $params["title"],
            "value" => $params["value"],
            "type" => $params["type"],
            "group" => $params["group"],
            "remark" => $params["remark"],
            "extra" => $params["extra"]
        ];
        $action = $this->request->action();
        try {
            validate(ConfigValidate::class)->scene($action)->check($params);
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
        $ret = $this->service->save($data);
        if ($ret) {
            $this->app->cache->clear();
            $this->success("保存成功");
        } else {
            $this->error("保存失败");
        }
    }

    public function edit()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get("id");
            if (!$id) {
                throw new ValidateException("缺少id");
            }
            $vo = $this->service->get($id);
            $this->assign("vo", $vo);
            return $this->fetch("form");
        }
        return $this->save();
    }

    public function status()
    {
        $params = $this->request->Post();
        try {
            validate(ConfigValidate::class)->scene("status")->check($params);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        $ret = $this->service->setStatus($params["id"], $params["status"]);
        if ($ret) {
            $this->app->cache->clear();
            $this->success("设置成功");
        } else {
            $this->error("设置失败");
        }
    }

    public function clear()
    {
        if (is_administrator() && $this->app->cache->clear()) {
            $this->success("清理成功");
        }
        $this->error("清理失败");
    }

    public function group()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get("type", 1);
            $type = config('global.CONFIG_GROUP_LIST');
            $list = $this->service->getAll(['status' => 1, 'group' => $id], "id,name,title,extra,value,remark,type", "sort asc");
            $this->assign('list', $list);
            $this->assign('type', $id);
            $this->assign("title", $type[$id] . '设置');
            return $this->fetch("group");
        }
        $config = $this->request->post("config");
        if ($config && is_array($config)) {
            foreach ($config as $name => $value) {
                $model = $this->service->getOne(["name" => $name]);
                $model->value = $value;
                $model->save();
            }
            $this->app->cache->clear();
        }
        $this->success("保存成功");
    }

    public function remove()
    {
        $params = $this->request->Post();
        if (empty($params["id"])) {
            throw new ValidateException("缺少id");
        }
        $config = $this->service->get($params["id"]);
        if (empty($config)) {
            throw new ValidateException("缓存不存在");
        }
        $this->service->delete($params["id"]);
        $this->app->cache->clear();
        $this->success("删除成功");
    }
}